#include "esp_uart2.h"

uint8_t  ESPGetTime  = 0;
uint16_t ESPGetCount = 0;

//检测应答命令
static uint8_t* ESP_CheckCmd(uint8_t *str){
	char *strx = 0;
	if(Usart2ReadFlag&0x8000)
	{ 
		Usart2ReadBuf[Usart2ReadFlag&0x7FFF]=0;//添加结束符
		strx=strstr((const char*)Usart2ReadBuf,(const char*)str);
	}
	return (uint8_t*)strx;
}

//cmd:发送的命令字符串
//ack:期待的应答结果,如果为空,则表示不需要等待应答
//返回值:0,发送成功  1,发送失败       
static uint8_t ESP_SendCmd(uint8_t *cmd,uint8_t *ack,uint16_t waittime) {
	uint8_t res = 0; 
	
    Usart2ReadFlag = 0;
    memset((void *)Usart2ReadBuf, 0 , sizeof(Usart2ReadBuf));

	ESP_Printf("%s\r\n",cmd);//发送命令
	if(ack&&waittime)		 //需要等待应答
	{
		while(--waittime)	 //等待倒计时
		{
			Delay_ms(10);
			if(Usart2ReadFlag&0x8000)
			{
				if(ESP_CheckCmd(ack))
                {        
                    Usart2ReadFlag = 0;                    
                    break; 
                }
                Usart2ReadFlag = 0;  
			}
		}
		if(waittime==0) res = 1;
	}
	return res;
}
//退出透传模式 0,退出成功; 1,退出失败
uint8_t ESP_QuitTrans(void){
    while(USART_GetFlagStatus(USART2,USART_FLAG_TC)==RESET){};
    USART_SendData(USART2,'+');
    Delay_ms(15);
    while(USART_GetFlagStatus(USART2,USART_FLAG_TC)==RESET){};
    USART_SendData(USART2,'+');
    Delay_ms(15);        
    while(USART_GetFlagStatus(USART2,USART_FLAG_TC)==RESET){};
    USART_SendData(USART2,'+');    
	Delay_ms(500);					//等待500ms
	return ESP_SendCmd((uint8_t *)"AT",(uint8_t *)"OK",20);//退出透传判断
}

uint8_t ESP_SendData(uint8_t *ch,uint16_t len){
    uint16_t i = 0;
    for( i = 0; i < len; i++)
    {
		while(USART_GetFlagStatus(USART2,USART_FLAG_TC)==RESET){}
        USART_SendData(USART2,ch[i]);
    }
	while(USART_GetFlagStatus(USART2,USART_FLAG_TC)==RESET){}
    return 1; 
}

//获取连接状态  0,未连接;1,连接成功.
uint8_t ESP_ConstaCheck(void){
    // 发送AT+CIPSTATUS指令,查询连接状态
	while(ESP_SendCmd((uint8_t *)"AT+CIPSTATUS",(uint8_t *)"OK",50));	
	return 0;
}

// STA模式下获取本地IP
uint8_t ESP_GetLocalIP(uint8_t* ipbuf){
	uint8_t *p,*p1;
    
    if(ESP_SendCmd((uint8_t *)"AT+CIFSR",(uint8_t *)"OK",50))
    {
        ipbuf[0] = 0;
        return 1;
    }
	
    p  = ESP_CheckCmd((uint8_t *)"\"");
    p1 = (uint8_t *)strstr((const char*)(p+1),"\"");
    *p1=0;
    sprintf((char*)ipbuf,"%s",p+1);
    return 0;
}
uint8_t ESP_ApInit(void){
    // 初始化esp8266所用串口
	Sys_Usart2Init(115200);
	
    // 延时2秒等待串口初始化完成 
	Delay_ms(1000);
    
    while(ESP_SendCmd((uint8_t *)"AT",(uint8_t *)"OK",200)){
        //退出透传
		ESP_QuitTrans();
      
        //关闭透传模式        	
        ESP_SendCmd((uint8_t *)"AT+CIPMODE=0",(uint8_t *)"OK",200);
		Delay_ms(1000);
	}
    
    // 关闭回显
    while(ESP_SendCmd((uint8_t *)"ATE0",(uint8_t *)"OK",200));	   
    
    // 设置波特率
    while(ESP_SendCmd((uint8_t *)"AT+UART=115200,8,1,0,0",(uint8_t *)"OK",200));
	
    Delay_ms(10);

    // 设置WIFI AP模式
    while(ESP_SendCmd((uint8_t *)"AT+CWMODE=2",(uint8_t *)"OK",200));
    while(ESP_SendCmd((uint8_t *)"AT+RST",(uint8_t *)"OK",200));	    
    
    // 延时4秒等待重启成功    
    Delay_ms(1000); 
    Delay_ms(1000);
    Delay_ms(1000); 
    Delay_ms(1000);
    
    ESP_SendCmd((uint8_t *)"AT+CWSAP=\"test001\",\"test5241\",1,4",(u8 *)"OK",1000);
    
    #ifdef TCP_Mode

    #endif
    #ifdef UDP_Mode
    ESP_SendCmd((uint8_t *)"AT+CIPMUX=0",(uint8_t *)"OK",200);       
    while(ESP_SendCmd((uint8_t *)"AT+CIPSTART=\"UDP\",\"192.168.4.2\",8086,8086,0",(uint8_t *)"OK",500));
    while(ESP_SendCmd((uint8_t *)"AT+CIPMODE=1",(uint8_t *)"OK",300));
    while(ESP_SendCmd((uint8_t *)"AT+CIPSEND",(uint8_t *)"OK",200));
    #endif    

    Usart2ReadFlag = 0;
    memset((void *)Usart2ReadBuf,0,sizeof(Usart2ReadBuf)); 
    return 1;
}
uint8_t ESP_StaConnect(void){
	if(ESP_SendCmd((uint8_t *)"AT+CWJAP=\"YY_12\",\"092413131Li\"",(uint8_t *)"WIFI GOT IP",100)){
		return 0;
	}
	else { 
        #ifdef TCP_Mode
            while(ESP_SendCmd((uint8_t *)"AT+CIPMUX=0",(uint8_t *)"OK",200));
            while(ESP_SendCmd((uint8_t *)"AT+CIPSTART=\"TCP\",\"192.168.0.102\",8086",(uint8_t *)"OK",200)); 
            while(ESP_SendCmd((uint8_t *)"AT+CIPMODE=1",(uint8_t *)"OK",200));
            while(ESP_SendCmd((uint8_t *)"AT+CIPSEND",(uint8_t *)"OK",20));
        #endif
        #ifdef UDP_Mode
            while(ESP_SendCmd((uint8_t *)"AT+CIPMUX=0",(uint8_t *)"OK",200));
            while(ESP_SendCmd((uint8_t *)"AT+CIPSTART=\"UDP\",\"192.168.0.102\",8086,8086,0",(uint8_t *)"OK",200)); 
            while(ESP_SendCmd((uint8_t *)"AT+CIPMODE=1",(uint8_t *)"OK",200));
            while(ESP_SendCmd((uint8_t *)"AT+CIPSEND",(uint8_t *)"OK",20));
        #endif
            
        Usart2ReadFlag = 0;
        memset((void *)Usart2ReadBuf,0,sizeof(Usart2ReadBuf));  
        return 1;
    }
}
uint8_t ESP_StaInit(void){
    // 初始化esp8266所用串口
	Sys_Usart2Init(115200);
	
    // 延时2秒等待串口初始化完成 
	Delay_ms(1500);
    while(ESP_SendCmd((uint8_t *)"AT",(uint8_t *)"OK",200)){
        //退出透传
		ESP_QuitTrans();
        
        //关闭透传模式        
		ESP_SendCmd((uint8_t *)"AT+CIPMODE=0",(uint8_t *)"OK",200);
		Delay_ms(800);
	}
    // 关闭回显
    while(ESP_SendCmd((uint8_t *)"ATE0",(uint8_t *)"OK",200));	   
    
    // 设置波特率
    while(ESP_SendCmd((uint8_t *)"AT+UART=115200,8,1,0,0",(uint8_t *)"OK",200));

	Delay_ms(10);
    
    // 设置WIFI STA模式
    while(ESP_SendCmd((uint8_t *)"AT+CWMODE=1",(uint8_t *)"OK",200));
    while(ESP_SendCmd((uint8_t *)"AT+RST",(uint8_t *)"OK",200));	 

    Delay_ms(1000);
    Delay_ms(1000);
    Delay_ms(1000);
    Delay_ms(1000);
    
    while(ESP_StaConnect()==0);
    
    return 1;  
}

void ESP_RecMessage(void) {
    if(Usart2ReadFlag&0x8000)
    {

        Usart2ReadFlag = 0;  
    }    
}

















